package pack1;

import java.io.IOException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swim.session.CollaborationRequestRemote;
import com.swim.session.SkillRemote;

/**
 * Gestisce il login degli utenti
 */
public class CollaborationRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public CollaborationRequestServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
    		response.sendRedirect("index.jsp");
			return;
    	}
    }
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
    		response.sendRedirect("index.jsp");
			return;
    	}
		String to = request.getParameter("to");
		try {
			String from = request.getParameter("from");

			if(from == null || to == null ) {
				String url ="/WEB-INF/auth/index.jsp";
				RequestDispatcher dispatcher = request.getRequestDispatcher(url);
				dispatcher.forward(request, response); 
			} else {
				response.setContentType("text/html");
				
				try {
					InitialContext jndiContext = (InitialContext) Util.getInitialContext();
					Object ref = jndiContext.lookup("CollaborationRequestBeanJNDI");
					CollaborationRequestRemote c = (CollaborationRequestRemote) ref;

					ref = jndiContext.lookup("SkillBeanJNDI");
					SkillRemote s = (SkillRemote)ref;
					String skillName = (String) request.getParameter("skill");
					c.newCollaborationRequest(from,to,s.getSkillByName(skillName));
					
					request.setAttribute("title","Collaboration Requested");
					request.setAttribute("message","The collaboration has been requested. Go <a href=\"profile.jsp?email="+to+"\">back</a> to the profile or <a href=\"index.jsp\">home</a>");
					RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
					dispatcher.forward(request, response); 
					
				} catch (NamingException e) {
					e.printStackTrace();
				}

			}
		} catch (Exception e) {

			request.setAttribute("title","Error requesting collaboration");
			request.setAttribute("message","<p>There was an error during the processing of your request.</p>" +
					"<p>Go to the <a href=\"profile.jsp?email="+to+"\">login</a> page to login or <a href=\"registration.jsp\">register</a> with another email.</p>");
			RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
			dispatcher.forward(request, response); 
		}
	}
	
}
